//alert(1)
$(document).ready(function() {
    function openAlertBg($current){
        $(".alert-bg.order-pay").show();
        $(".alert-bg #order-id").val($current.closest("tr").attr('data-id'));
        console.log($current.closest("tr").attr('data-id'));
        var userMoney=Number(_userInfo.money);
        currentAmount=Number($current.closest("tr").find(".total-fee span").text());
        var s="";
        if(userMoney<currentAmount){
            s="(账户余额"+userMoney+"元，余额支付"+userMoney+"元，其余"+(currentAmount-userMoney).toFixed(2)+"元使用支付宝)";
        }else{
            s="(账户余额"+userMoney+"元，余额支付"+currentAmount.toFixed(2)+"元)";
        }
        $(".alert-bg #pay-way-money-label").text(s);
    }
    $(".order-operation a.unpaid").click(function(e){
        e.preventDefault();
        openAlertBg($(this));
    });
    $(".alert-bg .close").click(function(e){
        e.preventDefault();
        $(this).closest(".alert-bg").hide();
        //clearInterval(ieCheck);
    });
    window.onload = function(){
        if($(".order-operation.current").length>0){
            openAlertBg($(".order-operation.current"));
        }
    }
    

    var codeInput = $('#test-code')
    var getCodeButton = $('.getCodeButton')
    var $submitButton=$('.info-affirm');
    var submitAble=false;
    var currentAmount=0;
    //发送验证码
    getCodeButton.click(function(e){
        e.preventDefault();
        //冻结
        if( _userInfo.money_usable != 1 ){
            alert("抱歉，您的余额支付已被冻结，您可以使用支付宝付款或联系客服");
            $("#alipay").prop('checked', true)
            return false;
        }

        var tel=_userBaseInfo.username;
        var type='pay';
        console.log(tel,type,currentAmount)
        api.solo('getAmountCode',[tel,type,currentAmount],function(d){
            alert(d.state==200?'验证码已发送请查收':d.title)

            //////////////

            //兼容弱智浏览器
            //alert(IS_IE9)
            var IS_IE9 = IS_IE9 || false;
            if(IS_IE9){
                //alert(1)
                var ieCheck = setInterval(function(){
                    //alert(1)
                    checkCode()
                },1500);
            }else{
                //alert(2)
            }
        })
    })

    //验证验证码
    
    codeInput.bind("input propertyChange", function(){
        checkCode()
    })
    function checkCode(){
        //$("#pay-way-money").prop("checked",true);
        var tel=_userBaseInfo.username;
        var code = codeInput.val();
        //alert(1)
        if(code.length!=6){
            $('.code-right').hide();
            $('.code-wrong').show();
            return false;
        }

        api.solo('checkCode',[tel,code],function(d){
            submitAble=false;
            //alert(submitAble)
            if(d.state==200){
                submitAble=true;
                $('.code-right').show();
                $('.code-wrong').hide();
            }else if(d.state==400) {
                $('.code-right').hide();
                $('.code-wrong').show();
                alert(d.title);
            }else{
                $('.code-right').hide();
                $('.code-wrong').show();
            }
        })
    }
    

    $submitButton.click(function(){
        //冻结
        if( $("#pay-way-money").is(':checked') && _userInfo.money_usable != 1 ){
            alert("抱歉，您的余额支付已被冻结，您可以使用支付宝付款或联系客服");
            $("#alipay").prop('checked', true)
            return false;
        }


        if(submitAble|| $("#alipay").prop("checked")){
            document.getElementById('order-pay').submit();
        }else{
            alert("验证码错误,请再次确认");
        }
    })
});
